package com.dream.technology.components.expresion.imp;

import com.dream.technology.components.context.FlowContext;
import com.dream.technology.components.expresion.ExpressionService;
import com.dream.technology.components.expresion.RiskExpressionAnaMatcher;
import com.dream.technology.db.entity.RcField;
import com.dream.technology.db.entity.RcFieldQuery;
import com.dream.technology.db.entity.RcRuleItem;
import com.dream.technology.db.entity.vo.RiskItemVo;
import com.dream.technology.db.entity.vo.VoRuleAll;
import com.dream.technology.service.RcFieldService;
//import com.dream.technology.zycashLog.Console;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 描述：规则解析
 *
 * @author chen_q_i@163.com
 * 2019/1/8 : 16:45.
 * @version : 1.0
 */
@Slf4j
@Component("ruleExpressionAnalysis")
public class RuleExpressionAnalysis {


    @Autowired
    private RiskExpressionAnaMatcher riskExpressionAnaMatcher;

    @Autowired
    private RcFieldService fieldService;


    public boolean analysisRule(VoRuleAll rule, FlowContext context) {

        Boolean eval = false;

        // 遍历标签的每一个配置
        Map<String, RcRuleItem> items = rule.getItems();
        String tagRule = rule.getRcRule().getRule();
        // 替换{}
        tagRule = tagRule.replaceAll("\\{", "(");
        tagRule = tagRule.replaceAll("}", ")");

//        for (RcCustomerTagItem item : customerTagItems) {
//            // 生成表达式 根据不同数据格式选择不同的执行
//            //分别有 数字1 ，boolean 2,radio 3,checkbox 4, datetime 5, string 6
//            Integer fieldDataType = item.getFieldDataType();
//            ExpressionService expressionServices = riskExpressionAnaMatcher.getExpressionServices(fieldDataType);
//            RiskItemVo voItem = item.getVoItem();
//            boolean b = expressionServices.expressionAnalysis(context, voItem);
//            // 别名替换
//            tagRule = tagRule.replace(item.getTagItemAlias(), String.valueOf(b));
//        }

        String exp = null;
        boolean result = false;
        for (Map.Entry<String, RcRuleItem> entry : items.entrySet()) {
            String k = entry.getKey();
            RcRuleItem value = entry.getValue();
            Integer fieldType = value.getFieldDataType();
            ExpressionService expressionServices = riskExpressionAnaMatcher.getExpressionServices(fieldType);
            RiskItemVo voItem = value.getVoItem();
            boolean valueIsNll = judgeValueIsNll(context, voItem);
            if (valueIsNll) {
                result = getNullPass(voItem);
                log.info("执行节点 ：{} 表达式 ：{} 结果 ：{}",voItem.getFieldName(),"值为空", result);
            } else {
                boolean b = expressionServices.expressionAnalysis(context, voItem);
                // 与动作计算 采用异或运算
//                exp = String.valueOf(b) +"&"+ (value.getRuleAction()>0);
                if (b) {
                    result = value.getRuleAction() > 0;
                } else {
                    result = !(value.getRuleAction() > 0);
                }
            }
        }
        return result;

    }


    /**
     * 判断是否为空值
     * @param flowContext
     * @param item
     * @return
     */
    public boolean judgeValueIsNll(FlowContext flowContext, RiskItemVo item) {
        Object o = flowContext.get(item.getFieldCode());
      return o==null;

    }


    /**
     * 获取空值策略通过还是拒绝
     * @param item
     * @return
     */
    public boolean getNullPass(RiskItemVo item) {
        RcFieldQuery query = new RcFieldQuery();
        query.createCriteria().andFieldCodeLike(item.getFieldCode());
        List<RcField> rcFields = fieldService.selectByExample(query);
        Boolean nullPass = rcFields.get(0).getNullPass();
        return nullPass;

    }

}
